package com.xhwl.data.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.exception.DataException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Position;
import com.xhwl.common.query.cent.sdata.PositionQuery;
import com.xhwl.data.service.IPositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author：lrp
 * @Description: 岗位管理
 * @Date: Create in 2022/01/19 10:00
 */
@RestController
@Slf4j
public class PositionController {

    @Autowired
    private IPositionService positionService;


    /**
     * 分页查询
     * @param positionQuery
     * @param page
     * @return
     */
    @GetMapping(value = "/position/page")
    public ResultJson page(PositionQuery positionQuery, Page page) {
        Page page1 = positionService.page(positionQuery, page);
        return ResultJson.success(page1);
    }


    /**
     * 列表查询
     * @param positionQuery
     * @return
     */
    @GetMapping(value = "/position/list")
    public ResultJson list(PositionQuery positionQuery) {
        List<Position> list = positionService.list(positionQuery);
        return ResultJson.success(list);
    }


    /**
     * 岗位名称批量校验
     * @param positions
     * @return
     */
    @PostMapping(value = "/position/name-check")
    public ResultJson verification(@RequestBody List<Position> positions) {
        return positionService.nameVerification(positions);
    }


    /**
     * 批量新增
     * @param positions
     * @return
     */
    @PostMapping(value = "/position/batch-create")
    public ResultJson batchCreate(@RequestBody List<Position> positions) {
        return positionService.batchCreate(positions);
    }


    /**
     * 新增
     * @param position
     * @return
     */
    @PostMapping(value = "/position/create")
    public ResultJson create(@RequestBody Position position) {
        if (null == position) {
            return ResultJson.fail("参数错误");
        }
        if (null != position.getId()) {
            return positionService.update(position);
        } else {
            return positionService.create(position);
        }
    }


    /**
     * 更新
     * @param position
     * @return
     */
    @PostMapping(value = "/position/update")
    public ResultJson update(@RequestBody Position position) {
        if (null == position && null == position.getId()) {
            return ResultJson.fail("数据错误");
        }
        return positionService.update(position);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping(value = "/position/delete")
    public ResultJson delete(Integer id) {
        return positionService.delete(id);
    }


    /**
     * 岗位列表数据导出
     * @param response
     * @param positionQuery
     * @throws IOException
     */
    @PostMapping(value = "/position/export")
    public void exportPosition(HttpServletResponse response, @RequestBody PositionQuery positionQuery) throws IOException {
        if (null == positionQuery.getEnterpriseId()) {
            throw new DataException("缺少企业id，数据异常");
        }
        positionService.positionExport(response, positionQuery);
    }


    /**
     * 岗位模板导出
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "/position/template-export")
    public void exportTemplate(HttpServletResponse response) throws IOException {
        positionService.exportTemp(response);
    }


    /**
     * 岗位管理导入
     * @param file
     * @param enterpriseId
     * @return
     */
    @PostMapping(value = "/position/import")
    public ResultJson positionImport(@RequestParam(value = "file", required = true) MultipartFile file,
                                     @RequestParam(value = "enterpriseId", required = true) Integer enterpriseId) throws Exception {
        return positionService.readExcel(file, enterpriseId);
    }
}
